/*
 * Copyright (c) 2007-2008, Thomas Krause (tho.krause@gmail.com)
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of gidoo nor the names of its contributors may be used to
 *       endorse or promote products derived from this software without specific
 *       prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.gidoo.wickettools;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.Model;

/**
 * This is a page containing a message box very similar to the ones used in desktop-applications
 * @author <a href="mailto:tho.krause@gmail.com">Thomas Krause</a>
 */
public class MessageBox extends WebPage
{
  
  public enum MessageBoxResult {YES, NO, ABORT};
  
  public enum MessageBoxButtons {OK, YES_NO, YES_NO_ABORT};
  
  private MessageBoxResult _result;
  private Page _returnPage;
  private IMessageBoxResultReceiver _receiver;
  
  /**
   * Simple constructor
   * @param text The message text displayed to the user. May not contain (X)HTML.
   * @param returnPage The page where to return to after the user made is choice.
   * @param receiver The receiver of the result of the users action.
   */
  public MessageBox(String text, Page returnPage, IMessageBoxResultReceiver receiver)
  {
    this(null, text, false, MessageBoxButtons.OK, returnPage, receiver);
  }
  
  /**
   * Complex constructor
   * @param title The title of the web page which is displayed to the user.
   * @param text The message text displayed to the user. May contain (X)HTML if
   * specified by the next parameter.
   * @param textIsHTML Wether the text should be escaped or not. If true it is not escaped.
   * @param buttons The choices that are displayed to the user (Ok, Yes, No, Abort, ...)
   * @param returnPage The page where to return to after the user made is choice.
   * @param receiver The receiver of the result of the users action.
   */
  public MessageBox(String title, String text,
    boolean textIsHTML,MessageBoxButtons buttons, Page returnPage,
    IMessageBoxResultReceiver receiver)
  {
    _result = MessageBoxResult.NO;
    _returnPage = returnPage;
    _receiver = receiver;
    
    if(title == null)
      title = getString("title");
    
    add(new Label("title", new Model(title)));
    
    Label lblText = new Label("text", new Model(text));
    lblText.setEscapeModelStrings(!textIsHTML);
    add(lblText);
    
    
    if(buttons == MessageBoxButtons.OK)
    {
      Fragment frag = new Fragment("buttons", "fragOk", this);
      
      Link lnkOk = new Link("lnkOk")
      {
        public void onClick()
        {
          _result = MessageBoxResult.YES;
          
          goAhead();
        }
      };
      
      lnkOk.add(new Label("lblOk", new Model(getString("lblOk"))));
      frag.add(lnkOk);
      add(frag);
    }
    else if(buttons == MessageBoxButtons.YES_NO || buttons == MessageBoxButtons.YES_NO_ABORT)
    {
      Fragment frag = new Fragment("buttons", "fragYesNoAbort", this);
      
      Link lnkYes = new Link("lnkYes")
      {
        public void onClick()
        {
          _result = MessageBoxResult.YES;
          goAhead();
        }
      };
      
      lnkYes.add(new Label("lblYes", new Model(getString("lblYes"))));
      frag.add(lnkYes);
      
      Link lnkNo = new Link("lnkNo")
      {
        public void onClick()
        {
          _result = MessageBoxResult.NO;
          goAhead();
        }
      };
      
      lnkNo.add(new Label("lblNo", new Model(getString("lblNo"))));
      frag.add(lnkNo);

      Link lnkAbort = new Link("lnkAbort")
      {
        public void onClick()
        {
          _result = MessageBoxResult.ABORT;
          goAhead();
        }
      };
      
      lnkAbort.add(new Label("lblAbort", new Model(getString("lblAbort"))));
      frag.add(lnkAbort);
      
      if(buttons == MessageBoxButtons.YES_NO_ABORT)
      {
        lnkAbort.setVisible(true);
      }
      else
      {
        lnkAbort.setVisible(false);
      }
      
      add(frag);
    }
  }
  
  private void goAhead()
  {
    if(_receiver != null)
    {
      _receiver.receiveMessageBoxResult(_result);
    }
    setResponsePage(_returnPage);
  }
  
  /**
   * Get the result. Depends on the button the user has clicked.
   * @return The result.
   */
  public MessageBoxResult getResult()
  {
    return _result;
  }

  
}
